package com.zsh.myserver.core.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2020-2020
 * Author: Zhoushaohua
 * Date: 2020/4/11 21:00
 * Description: 异常码配置文件管理实体类
 */
@ConfigurationProperties(prefix = "server")
@PropertySource(value = "classpath:config/exception_code.properties")
@Component
public class ExceptionCodeConfiguration {
    private Map<Integer, String> codes = new HashMap<>();

    public String getExceptionMessage(int code) {
        if (codes.isEmpty() || !codes.containsKey(code)) {
            return "没有找到对应的消息，请检查异常码配置文件；当前 code : " + code;
        }
        return codes.get(code);
    }

    public Map<Integer, String> getCodes() {
        return codes;
    }

    public void setCodes(Map<Integer, String> codes) {
        this.codes = codes;
    }
}
